// 头像工具函数
// 为客户和经理提供专业商务风格的头像

// 中国人商务风格的头像URL集合 - 按性别分类
// 使用高质量的亚洲商务人士头像，适合金融理财场景
const BUSINESS_AVATARS = {
  managers: {
    male: [
      // 专业亚洲男性商务头像
      'https://images.unsplash.com/photo-1558222218-b7b54eede3f3?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1603415526960-f7e0328c63b1?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1612349317150-e413f6a5b16d?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1633332755192-727a05c4013d?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=150&h=150&fit=crop&crop=face',
    ],
    female: [
      // 专业亚洲女性商务头像
      'https://images.unsplash.com/photo-1594736797933-d0501ba2fe65?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1551836022-d5d88e9218df?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1598300042247-d088f8ab3a91?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1619895862022-09114b41f16f?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1580489944761-15a19d654956?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1607990281513-2c110a25bd8c?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1551836022-8b2858c9c69b?w=150&h=150&fit=crop&crop=face',
    ]
  },
  customers: {
    male: [
      // 多样化的亚洲男性客户头像
      'https://images.unsplash.com/photo-1582750433449-648ed127bb54?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1566492031773-4f4e44671d66?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1558222218-b7b54eede3f3?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1603415526960-f7e0328c63b1?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1612349317150-e413f6a5b16d?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1633332755192-727a05c4013d?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=150&h=150&fit=crop&crop=face',
    ],
    female: [
      // 多样化的亚洲女性客户头像
      'https://images.unsplash.com/photo-1594736797933-d0501ba2fe65?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1551836022-d5d88e9218df?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1598300042247-d088f8ab3a91?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1619895862022-09114b41f16f?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1580489944761-15a19d654956?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1607990281513-2c110a25bd8c?w=150&h=150&fit=crop&crop=face',
      'https://images.unsplash.com/photo-1551836022-8b2858c9c69b?w=150&h=150&fit=crop&crop=face',
    ]
  }
};

// 常见的中文名字性别判断
const COMMON_CHINESE_NAMES = {
  male: [
    '明', '强', '华', '伟', '军', '杰', '涛', '磊', '超', '刚', '勇', '毅', '俊', '峰', '辉', '鹏', '斌', '健', '威', '龙'
  ],
  female: [
    '丽', '薇', '娜', '敏', '静', '雅', '芳', '燕', '红', '梅', '玲', '琳', '慧', '倩', '婷', '欣', '怡', '萍', '蓉', '洁'
  ]
};

/**
 * 根据中文名字猜测性别
 */
const guessGender = (name: string): 'male' | 'female' => {
  if (!name) return 'male';
  
  const lastName = name.charAt(name.length - 1);
  
  // 检查是否包含常见女性名字字符
  if (COMMON_CHINESE_NAMES.female.includes(lastName)) {
    return 'female';
  }
  
  // 检查是否包含常见男性名字字符
  if (COMMON_CHINESE_NAMES.male.includes(lastName)) {
    return 'male';
  }
  
  // 根据一些常见的女性名字字符进行判断
  const femaleChars = ['女', '妹', '姐', '姨', '娟', '秀', '美', '花', '月', '香', '珠', '凤', '英', '兰', '菊', '梅', '莲', '翠', '霞','Alice'];
  const maleChars = ['男', '哥', '叔', '伯', '君', '王', '帝', '豪', '雄', '虎', '龙', '鹏', '翔', '飞', '武', '文', '德', '正', '义', '仁'];
  
  for (let char of femaleChars) {
    if (name.includes(char)) return 'female';
  }
  
  for (let char of maleChars) {
    if (name.includes(char)) return 'male';
  }
  
  // 默认返回男性
  return 'male';
};

// 经理头像映射（包含性别信息）
const MANAGER_INFO: { [key: string]: { name: string; gender: 'male' | 'female' } } = {
  'B_Admin001': { name: '张明', gender: 'male' },
  'B_Admin002': { name: '王丽', gender: 'female' },
  'B_Admin003': { name: '李华', gender: 'male' },
  'B_Manager_001': { name: '赵强', gender: 'male' },
  'B_Manager_002': { name: '陈薇', gender: 'female' },
};

/**
 * 根据经理ID获取头像URL
 */
export const getManagerAvatar = (managerId: string): string => {
  try {
    const managerInfo = MANAGER_INFO[managerId];
    if (!managerInfo) {
      return BUSINESS_AVATARS.managers.male[0];
    }
    
    const gender = managerInfo.gender;
    const avatars = BUSINESS_AVATARS.managers[gender];
    const hash = managerId.charCodeAt(managerId.length - 1);
    const index = hash % avatars.length;
    
    return avatars[index];
  } catch (error) {
    // 如果出错，返回第一个男性头像作为默认值
    console.warn('获取经理头像失败:', error);
    return BUSINESS_AVATARS.managers.male[0];
  }
};

/**
 * 根据客户ID和名字获取头像URL
 */
export const getCustomerAvatar = (customerId: number, customerName?: string): string => {
  try {
    const gender = customerName ? guessGender(customerName) : 'male';
    const avatars = BUSINESS_AVATARS.customers[gender];
    const index = customerId % avatars.length;
    return avatars[index];
  } catch (error) {
    // 如果出错，返回第一个男性客户头像作为默认值
    console.warn('获取客户头像失败:', error);
    return BUSINESS_AVATARS.customers.male[0];
  }
};

/**
 * 根据名字生成首字母头像的背景色
 */
export const getAvatarColor = (name: string): string => {
  const colors = [
    '#1890ff', '#52c41a', '#722ed1', '#eb2f96', '#f5222d',
    '#fa541c', '#faad14', '#13c2c2', '#2f54eb', '#722ed1'
  ];
  
  const hash = name.split('').reduce((acc, char) => {
    return acc + char.charCodeAt(0);
  }, 0);
  
  return colors[hash % colors.length];
};

/**
 * 创建初始头像样式
 */
export const createInitialAvatar = (name: string, size: number = 40) => {
  const initials = name.charAt(0).toUpperCase();
  const backgroundColor = getAvatarColor(name);
  
  return {
    backgroundColor,
    color: '#fff',
    fontSize: `${size * 0.4}px`,
    fontWeight: 'bold',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    width: size,
    height: size,
    borderRadius: '50%',
    children: initials
  };
};

/**
 * 获取带有错误处理的头像URL
 * 如果图片加载失败，会回退到首字母头像
 */
export const getAvatarWithFallback = (
  url: string, 
  _fallbackName: string, 
  onError?: () => void
) => {
  const img = new Image();
  img.onload = () => {
    // 图片加载成功
  };
  img.onerror = () => {
    // 图片加载失败，触发回调
    if (onError) onError();
  };
  img.src = url;
  
  return url;
};

/**
 * 预加载头像图片
 */
export const preloadAvatars = () => {
  const allUrls = [
    ...BUSINESS_AVATARS.managers.male,
    ...BUSINESS_AVATARS.managers.female,
    ...BUSINESS_AVATARS.customers.male,
    ...BUSINESS_AVATARS.customers.female,
  ];
  
  allUrls.forEach(url => {
    const img = new Image();
    img.src = url;
  });
}; 